package com.nicolasprof.profgallery.viewer {
	import flash.display.DisplayObjectContainer;	
	import flash.display.DisplayObject;	

	import com.nicolasprof.profgallery.effects.Effect;	
	import com.nicolasprof.profgallery.effects.EffectManager;	
	import flash.display.Bitmap;	
	import flash.display.BitmapData;	
	import flash.display.Sprite;
	
	/**
	 * @author Nicolas Prof
	 */
	public class Layer extends Sprite {
		public function Layer() {
		}
		public function assignBitmapData($data : BitmapData) : void {
			var bmp : Bitmap = new Bitmap($data, "auto", true);
			var overLayer : Sprite = new Sprite();
			var em : EffectManager = EffectManager.getInstance();
			var effect : Effect = em.getEffect();
			
			this.addChild(bmp);
			this.addChild(overLayer);
			
			effect.build(bmp, overLayer);
		}

		public function clear() : void {
			clearBitmap(this.getChildAt(0));
			var sprite : Sprite = this.getChildAt(1) as Sprite;
			sprite.graphics.clear();
			while(sprite.numChildren) {
				sprite.removeChildAt(0);
			}
		}
		private function clearBitmap($layer : DisplayObject) : void {
			if($layer is Bitmap) {
				Bitmap($layer).bitmapData.dispose();
			}else{
				clearBitmap(DisplayObjectContainer($layer).getChildAt(0));
			}
		}
	}
}
